#include "coverrideclient.h"
#include "decorations/cshadowdecoration.h"

namespace buola { namespace bwm {

COverrideClient::COverrideClient(Window pID,const XWindowAttributes &pXAttrs)
    :   CClient(pID,pXAttrs)
{
    //because we don't know if it was damaged before we got the MapNotify
    SetFlag(FLAG_DAMAGED);
    mZPriority=ZP_OVERRIDE;
}

COverrideClient::~COverrideClient()
{
}

void COverrideClient::CreateDecoration()
{
    msg_info() << "creating decoration " << (int)mWindowType << "\n";
    if(mWindowType==gui::EWindowType::DROPDOWNMENU||mWindowType==gui::EWindowType::POPUPMENU||
            mWindowType==gui::EWindowType::TOOLTIP||mWindowType==gui::EWindowType::COMBO)
        mDecoration=new CShadowDecoration(this);
}

void COverrideClient::OnConfigureNotify(XConfigureEvent *pE)
{
    CSize_i lOldSize=mPhysicalSize;
    mBorderWidth=pE->border_width;
    mPhysicalPos={pE->x,pE->y};
    mPhysicalSize={pE->width+2*mBorderWidth,pE->height+2*mBorderWidth};
    mXPos=mPhysicalPos;
    OnGeometryChanged(lOldSize!=mPhysicalSize);
}

void COverrideClient::OnMapNotify(bool pWMStarting)
{
    StartComposer();
    SetFlag(FLAG_MAPPED);

    //move it to current desktop
    ReadCommonProperties();
    CalcDesktops();

    if(!mDecoration)
        CreateDecoration();
    
    if(!pWMStarting&&(mWindowType==gui::EWindowType::DROPDOWNMENU||mWindowType==gui::EWindowType::POPUPMENU||
            mWindowType==gui::EWindowType::TOOLTIP||mWindowType==gui::EWindowType::COMBO))
    {
        if(mAnimating!=ANIM_DEICONIFY)
            StartAnimation(ANIM_FADEIN);
    }
    else
    {
        mEffOpacity=mOpacity;
    }

    if(mDecoration)
        mDecoration->OnClientMapped();

    DetermineRegions();
    DetermineMode();
}

void COverrideClient::OnUnmapNotify(bool pSynthetic)
{
    UnsetFlag(FLAG_MAPPED);

    if(mDecoration)
        mDecoration->OnClientUnmapped();

    if(mAnimating!=ANIM_ICONIFY)
        StartAnimation(ANIM_FADEOUT);
}

void COverrideClient::OnDestroyNotify()
{
    mID=0;
    mXDamage=0;

    StartAnimation(ANIM_FADEOUT);
}

/*namespace bwm*/ } /*namespace buola*/ }
